package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.json.JSONObject
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @Title: 外部系统嵌入工单界面API* @author: LiJuntao* @date 2021-07-13 23:58:55
 *
 */

public class apiDynLogin implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiDynLogin.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
        String DOMAIN = KHDBConfig.getParam(eliteBean, "NGSDOM");

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

        String getMethod = request.getMethod();
        log.debug("getMethod:" + getMethod);
        String dataSourceID = "dataSource";

        //获取所有参数名
        Enumeration e = request.getParameterNames();
        while (e.hasMoreElements()) {
            //获取当前参数名
            String parameterName = (String) e.nextElement();
            //再通过request.getParameter("")的方法来获取对应参数名的值
            log.debug(parameterName + ": " + request.getParameter(parameterName));
        }

        // 获取嵌入传递的参数

        String mini_id = request.getParameter("openid");
        String dynid = request.getParameter("dynid");
        String opsource = request.getParameter("opsource");
        String expand = request.getParameter("expand");
        String usertype = "";
        String userid = "";

        // 非空校验检查检查
        log.debug("mini_id.length()" + mini_id.length());

        if (KHUtil.isNull(mini_id) || KHUtil.isNull(dynid) || KHUtil.isNull(opsource) || mini_id.length() < 20) {
            return "登录信息异常OpenID:" + mini_id;
        } else {

            String dealSQL = "select customer_guid as userid, loginname, 'customer' as usertype, 2 as sno from customer where mini_id = ? order by sno limit 1";
            List<Map<String, Object>> selectExits = jt.queryForList(dealSQL, mini_id);


            if (selectExits.size() > 0) {
                Map selectExitsm = selectExits.get(0);
                userid = selectExits[0].getAt("userid");

                usertype = selectExitsm.get("usertype");
            } else {
                userid = mini_id;
                usertype = "notype";
            }
            //获取NGS菜单短码
            String apiLinkJson = Util.doHttpGet(DOMAIN + "/ngsapitask_ph/api/apiShortenLink?dynid=" + dynid + "&userid=" + userid + "&usertype=" + usertype + "&woid=" + "" + "&taskid=" + "" + "&expand=&cbtype=&opsource=" + opsource, "UTF-8",
                    5000, 0);
            JSONObject reLinkJson = new JSONObject(apiLinkJson);
            log.debug("获取的短链接地址:" + reLinkJson.toString());
            String linkURL = KHUtil.GetJV(reLinkJson, "message");

            response.sendRedirect(linkURL);

        }

    }

}